package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class DishController {
    @Autowired
    DishService dishService;

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/dish/list")
    public ResultInfo list(Long categoryId,Integer status){
        List<Dish> list = null;
        //1.先查询redis缓存
        //1-1设置rediskey
        String dishKey = "dish_"+categoryId;

        if (redisTemplate.hasKey(dishKey)) {
            list = (List<Dish>) redisTemplate.opsForValue().get(dishKey);
        }else{
            list = dishService.list(categoryId,status);
            redisTemplate.opsForValue().set(dishKey,list);
        }
        return ResultInfo.success(list);
    }
}
